'use strict';

/**
 * @ngdoc directive
 * @name jgappApp.directive:cusSelect
 * @description
 * # cusSelect
 */
angular.module('jgappApp')
    .directive('cusSelect', function ($rootScope, $location) {
        return {
            scope: {
                sheetObj: "=sheetData"
            },
            template: '<div class="sheetAction">' +
            '<div class="sheet-top flex">' +
            '<div class="sheet-cancel tl c9" ng-click="sheetCancel()">取消</div>' +
            '<div class="sheet-title tc">{{sheetObj.sheettitle}}</div>' +
            '<div class="sheet-confirm tr" ng-click="sheetConfirm()">确定</div>' +
            '</div>' +
            '<div class="sheet-list tc">' +
            '<div class="sheet-item" ng-click="onClickTab(item)" ng-class="{active:item.selected}" ng-repeat="item in sheetObj.tabList">{{item.key}}</div>' +
            '</div>' +
            '</div>',
            restrict: 'E',
            link: function postLink(scope, element, attrs) {
                scope.onClickTab = function (item) {
                    item.selected = !item.selected;
                };
                scope.sheetCancel = function () {
                    closeSheet()
                };
                scope.sheetConfirm = function () {
                    // 通过当前选项判断选择了哪个
                    var type = scope.sheetObj.type;
                    var sheetObj = scope.sheetObj[type];
                    var selectedData = [];
                    selectedData = sheetObj.tabList.filter(function (item, index) {
                        return item.selected;
                    })
                    if (sheetObj.callback && selectedData.length > 0) {
                        sheetObj.callback(selectedData)
                    }
                    closeSheet()

                };
                // 重置选择状态
                function _resetData() {
                    var type = scope.sheetObj.type
                    var sheetObj = scope.sheetObj[type]
                    sheetObj.tabList.forEach(function (item, index) {
                        item.selected = false;
                    });
                }
                var closeSheet = function () {
                    _resetData()
                    scope.sheetObj.ifshowSheet = false
                    $rootScope.showMasker = false
                };


                // element.text('this is the sheetAction directive');
            }
        };
    });
